package com.wgchao.identityrightsmanagement.query.vo;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceProject;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountRepository;
import com.wgchao.identityrightsmanagement.infrastructure.mapstruct.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @ClassName: ProjectVoConvert
 * @author: wgchao
 * @createTime: 2020/6/18 5:42 PM
 * @Description: TODO
 */
@Component
public class ProjectVoConvert {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private SysBaseAccountRepository sysBaseAccountRepository;

    public ProjectVo convert(SysResourceProject sysResourceProject){
        ProjectVo projectVo = projectMapper.toT(sysResourceProject);
        if(!StringUtils.isEmpty(sysResourceProject.getCreator())){
            SysBaseAccount creator = sysBaseAccountRepository.findFirstByUsername(sysResourceProject.getCreator());
            projectVo.setCreatorName(creator == null ? null : creator.getName());
        }
        if(!StringUtils.isEmpty(sysResourceProject.getLastUpdator())){
            SysBaseAccount updator = sysBaseAccountRepository.findFirstByUsername(sysResourceProject.getLastUpdator());
            projectVo.setLastUpdatorName(updator == null ? null : updator.getName());
        }
        return projectVo;
    }
}
